/*
 * @Author: liu wang wang
 * @Date: 2020-07-07 17:32:39
 * @LastEditTime: 2020-08-14 17:17:18
 * @LastEditors: liu wang wang
 * @Description: St接口类
 * @FilePath: \st-sdk-js\src\core.ts
 */
import Bone from './utils/Bone';
import {
    setGameServer,
    info,
    edit,
    choose,
    alert,
    concat
} from './components/index';
import { openType, popType } from './config/index';
import {
    alertInterface,
    StInterface,
    OpenInterface,
    defaultInitInterface,
    infoInitInterface,
    chooseInterface,
    editInterface,
    copyInterface
} from '../types/index'
import Common from './utils/Common';
import api from './api';
import store from './utils/store';
import msg from './utils/Msg';
const oBone = new Bone({
    width: '500px'
});
class St extends Common {
    protected theme: string;
    protected app: string;
    protected signUrl: string;
    constructor({ theme, app, signUrl }: StInterface) {
        super();
        this.theme = theme;
        this.app = app;
        this.signUrl = signUrl;
        this.getSign();
    }
    // 获取签名
    protected getSign(): void {
        store.setStore('signUrl',this.signUrl);
        api.getSign(this.signUrl, {
            success: function (res: any) {
                if(!res || (typeof res === 'string' && res.length>=200)){
                    throw new Error('签名获取失败');
                }else{
                    store.setStore('sign', res);
                }
            }
        });
    }
    // 统一弹框
    protected layout(bone: HTMLElement): void {
        const stLayout: HTMLElement | null = document.getElementById('st-layout-shadow');
        if (stLayout !== null) {
            (stLayout.parentNode as HTMLElement).removeChild(stLayout);
        }
        const layout = this.createDiv();
        layout.id = 'st-layout-shadow';
        this.setStyle(layout, 'width:100%;height:100vh;position:fixed;top:0;left:0;background:rgba(0, 0, 0, 0.5);z-index:99');
        layout.appendChild(bone);
        document.body.appendChild(layout);
    }
    // 默认弹窗初始化
    protected defaultInit(options: defaultInitInterface): void {
        this.layout(oBone.init(setGameServer.init({ ...options })));
    }
    // 版本资料弹框初始化
    protected infoInit(initOptions: infoInitInterface): void {
        this.layout(oBone.init(info.init({ ...initOptions })))
    }
    // 编辑弹框初始化
    protected editInit({ theme, zoneID, versionID, success }: editInterface): void {
        this.layout(oBone.init(edit.init({ theme, zoneID, versionID, success})))
    }
    // 选择版本初始化
    protected chooseInit(chooseOPtions: chooseInterface): void {
        this.layout(oBone.init(choose.init(chooseOPtions), '选择版本'));
    }
    // 受保护的逻辑调用
    protected _open({ type, data, success, error, gotoCreate, editFun, setOtherFun, close }: OpenInterface): void {
        store.setStore('close',close);
        switch (type) {
            case openType.CREATE:
                this.defaultInit({
                    theme: this.theme,
                    zoneID: (data.zoneid as number),
                    versionID: (data.versionid as number),
                    success: (res: any) => {
                        success && success(res);
                    }
                });
                break;
            case openType.INFO:
                this.infoInit({
                    theme: this.theme,
                    app: this.app,
                    zoneID: data.zoneid,
                    versionID: data.versionid,
                    edit: (editFun as Function),
                    setOther: (setOtherFun as Function),
                    success: (res: any) => {
                        success && success(res);
                    }
                });
                break;
            case openType.EDIT:
                this.editInit({
                    theme: this.theme,
                    versionID: data.versionid,
                    zoneID: data.zoneid,
                    success: (res: any) => {
                        success && success(res);
                    }
                });
                break;
            case openType.CHOOSE:
                this.chooseInit({
                    theme: this.theme,
                    success: (res: any) => {
                        success && success(res);
                    },
                    gotoCreate: () => {
                        gotoCreate && gotoCreate();
                    }
                })
                break;
            default:
                this.defaultInit({
                    theme: this.theme,
                    zoneID: data.zoneid,
                    versionID: (data.versionid as number),
                    success: (res: any) => {
                        success && success(res);
                    }
                });
        }
    }
    // 逻辑操作框
    public open({ check = false, type = 'create', data, success, error, gotoCreate, editFun, setOtherFun, close}: OpenInterface): void {
        if (check) {
            api.checkHasVersion({
                params: {
                    type: this.app,
                    zoneID: data.zoneid,
                    versionID: data.versionid
                },
                success: (res: any) => {
                    if(res.code===401){
                        if(type === openType.SUBMIT){
                            msg.msg('账号无权限')
                        }
                        success && success();
                        return false;
                    }
                    let lastType = type;
                    const pop = res.data.popType;
                    store.setStore('zoneid',res.data.zoneID);
                    switch (pop) {
                        case popType.CREATE:
                            type = 'create';
                            break;
                        case popType.SURE_EDIT:
                            type = 'info';
                            break;
                        case popType.SURE:
                            type = 'info';
                            break;
                        case popType.NOPOP:
                            if (type === openType.SUBMIT) {
                                type = 'info';
                            }
                            break;
                        default:
                            break;
                    }
                    if (pop === popType.NOPOP && lastType !== openType.SUBMIT) {
                        success && success();
                        return false;
                    }
                    this._open({ type, data, success, error, gotoCreate, editFun, setOtherFun, close });
                }
            })
        } else {
            this._open({ type, data, success, error, gotoCreate, editFun, setOtherFun, close });
        }
    }
    // 提示框
    public alert({ title = '提示', content, btn, success, close }: alertInterface): void {
        store.setStore('close', close);
        let bone = oBone.init(alert.init({ theme: this.theme, content, btn, success, close }), title);
        this.setStyle(bone, 'width:300px;');
        this.layout(bone);
    };
    // 在线客服弹框
    public concat(text: string, close?:Function): void {
        store.setStore('close', close);
        let bone = oBone.init(concat.init({ theme: this.theme, text }), '在线客服');
        this.setStyle(bone, 'width:450px;');
        this.layout(bone);
    }
    // 复制
    public copy(options:{data:any,success:Function}, close?: Function): void {
        store.setStore('close', close);
        api.copy({
            ...options
        })
    }
    // 关闭弹窗
    public close(): void {
        super.close();
    }

}

export default St;